//
//  EmoImgView.swift
//  YueChat
//
//  Created by IMac on 2024/9/23.
//

import UIKit

class EmoImgView: UIView, UICollectionViewDelegate, UICollectionViewDataSource {

    var data = [String]()

    var emoClickBlock: (_ str:String) -> Void = {str in }
    var emoSendBlock: () -> Void = {}
    
    lazy var layout: UICollectionViewFlowLayout = {
        
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = CGSize(width: kScreenWidth/7, height: 44)
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.scrollDirection = .vertical
        layout.sectionInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
        return layout
    }()
    
    lazy var collectionView: UICollectionView = {
        let collectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height:  300), collectionViewLayout: layout)
        collectionView.dataSource = self
        collectionView.backgroundColor = kGrayBG
        collectionView.delegate = self
        self.addSubview(collectionView)
        return collectionView
    }()
    
    lazy var sendBtn : UIButton = {
        let btn = UIButton(type: .system)
        btn.frame = CGRect(x: kScreenWidth - 80, y: 300 - 80, width: 60, height: 40)
        btn.setTitle("发送", for: .normal)
        btn.tintColor = UIColor.white
        btn.titleColor(for: .normal)
        btn.layer.cornerRadius = 5
        btn.addTarget(self, action: #selector(sendClick), for: .touchUpInside)
        btn.backgroundColor = kMainColor
        return btn
    }()
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.frame = CGRect(x: 0, y: kScreenHeight - 300, width: kScreenWidth, height: 300)
        self.backgroundColor = kGrayBG
        self.loadData()
        self.addSubview(collectionView)
        self.addSubview(sendBtn)
        collectionView.register(UINib(nibName: "EmoImgCell", bundle: nil), forCellWithReuseIdentifier: "EmoImgCell")
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc func sendClick () {
        emoSendBlock()
    }
    
    func loadData() {
        if let mainBundle = Bundle.main.resourceURL {
            let resourcePath = mainBundle.appendingPathComponent("EmojList.plist")
            if let data = NSArray(contentsOf: resourcePath) {
                self.data = data as! [String]
                self.collectionView.reloadData()
            }
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return data.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "EmoImgCell", for: indexPath) as! EmoImgCell
        let info = data[indexPath.row]
        cell.lab.text = info
        cell.lab.font = UIFont(name: "AppleColorEmoji", size: 30)
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        emoClickBlock(self.data[indexPath.row])
    }

}
